package priv.lhy.user.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import priv.lhy.constants.ResponseCodeEnum;
import priv.lhy.constants.ServiceException;

/**
 * author : lihy
 * date : 2019/1/30 13:02
 * <p>
 * 异常处理类
 */
public class ExceptionUtil {
    private static Logger logger = LoggerFactory.getLogger(Exception.class);

    /**
     * 将下层抛出的异常转换为response返回码
     *
     * @param e
     * @return
     */
    public static Exception handlerException4biz(Exception e) {
        Exception ex = null;
        if (!(e instanceof Exception)) {
            return null;
        }
        if (e instanceof ValidateException) {
            //验证异常信息
            ex = new ServiceException(((ValidateException) e).getErrorCode(), ((ValidateException) e).getErrorMessage());
        } else if (e instanceof Exception) {
            //非指定异常
            ex = new ServiceException(ResponseCodeEnum.SYSTEM_BUSY.getCode(), ResponseCodeEnum.SYSTEM_BUSY.getMsg());
        }
        logger.error("ExceptionUtil.handlerException4biz, Exception=" + e.getMessage(), e);
        return ex;
    }
}
